<template>
  <div class="flex flex-col p-3">
    <van-button @click="closeWindow()">关闭ui窗口</van-button>
    <van-button class="mt-2" @click="hideWindow()">隐藏ui窗口，3s后重新弹出</van-button>
    <van-button class="mt-2" @click="testPublicFunc()">测试公共函数</van-button>

    <div>时间戳: {{ timeStamp }}</div>
    <van-button class="mt-2" @click="getValue()">获取</van-button>
    <van-button class="mt-2" @click="setValue()">设置</van-button>

    <div>isConnected： {{ isConnected }}</div>

    <van-button class="mt-2" :disabled="isConnected" @click="connect()">连接</van-button>

    <van-button class="mt-2" @click="task1()">偏移点击</van-button>

    <van-button class="mt-2" @click="test()">测试</van-button>

    <van-button class="mt-2" @click="release()">释放</van-button>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue';

// const props = defineProps({
//   show: {
//     type: Boolean,
//     required: true,
//   }
// });
const emit = defineEmits<{
  (e: 'update:show', show: boolean): void
  (e: 'submit'): void
}>();

const timeStamp = ref('-')

function closeWindow() {
  window.airscript?.close()
}
function hideWindow() {
  window.airscript?.close()
  window.airscript?.call('delayShow', '')
}
function testPublicFunc() {
  window.airscript?.call('testPublicFunc', '')
}

function getValue() {
    timeStamp.value = window.airscript?.get('timeStamp');
}

function setValue() {
    window.airscript?.save('timeStamp', new Date().getTime().toString());
}

let port = ref();
let reader;

const isConnected = computed(() => {
  return !!port.value?.connected;
})

/** 
 * 连接
 */
async function connect() {
  if (port.value?.connected) return console.log('已连接');
  port.value = await (navigator as any).serial.requestPort();
  await port.value.open({ baudRate: 9600 });
}

/**
 * 发送单条命令
 * await sendCommand(KeysMap.MOUSE_LEFT)
 * await sendCommand(KeysMap.MOUSE_RELEASE)
 */
async function sendCommand(command) {
  const writer = port.value.writable.getWriter();
  await writer.write(new Uint8Array(command));
  writer.releaseLock();
}

/** 按键大全 */
const KeysMap = {
  // 鼠标左键按下
  MOUSE_LEFT: [0x57, 0xAB, 0x00, 0x04, 0x07, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10],
  // 鼠标右键按下
  MOUSE_RIGHT: [0x57, 0xAB, 0x00, 0x04, 0x07, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11],
  // 鼠标释放
  MOUSE_RELEASE: [0x57, 0xAB, 0x00, 0x04, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F],

  // 普通键键释放
  KEY_RELEASE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C],
  
  // 字母键
  A: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10],
  B: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11],
  C: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12],
  D: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13],
  E: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14],
  F: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15],
  G: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16],
  H: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17],
  I: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18],
  J: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19],
  K: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A],
  L: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B],
  M: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C],
  N: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1D],
  O: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E],
  P: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F],
  Q: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20],
  R: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21],
  S: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22],
  T: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23],
  U: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24],
  V: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25],
  W: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26],
  X: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27],
  Y: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28],
  Z: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29],

  // 数字键
  NUM_1: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2A],
  NUM_2: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B],
  NUM_3: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C],
  NUM_4: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2D],
  NUM_5: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E],
  NUM_6: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2F],
  NUM_7: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30],
  NUM_8: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31],
  NUM_9: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32],
  NUM_0: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33],

  // ============= 功能键区 =============
  /** 回车键 */
  ENTER: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34],
  /** ESC取消键 */
  ESC: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35],
  /** 退格键 */
  BACKSPACE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36],
  /** 制表符键 */
  TAB: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37],
  /** 空格键 */
  SPACE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38],

  // ============= 符号键 =============
  /** -减号键 */
  MINUS: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39],
  /** =等号键 */
  EQUAL: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3A],
  /** [左方括号键 */
  LEFT_BRACKET: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B],
  /** ]右方括号键 */
  RIGHT_BRACKET: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C],
  /** \反斜杠键 */
  BACKSLASH: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3D],
  /** ;分号键 */
  SEMICOLON: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F],
  /** '单引号键 */
  QUOTE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40],
  /** `反引号键 */
  BACKTICK: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41],
  /** ,逗号键 */
  COMMA: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42],
  /** .句号键 */
  PERIOD: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43],
  /** /斜杠键 */
  SLASH: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44],
  /** 大小写锁定键 */
  CAPSLOCK: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45],

  // ============= F1-F12功能键 =============
  /** F1功能键 */
  F1: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46],
  /** F2功能键 */
  F2: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47],
  /** F3功能键 */
  F3: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48],
  /** F4功能键 */
  F4: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49],
  /** F5功能键 */
  F5: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4A],
  /** F6功能键 */
  F6: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4B],
  /** F7功能键 */
  F7: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4C],
  /** F8功能键 */
  F8: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D],
  /** F9功能键 */
  F9: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E],
  /** F10功能键 */
  F10: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F],
  /** F11功能键 */
  F11: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50],
  /** F12功能键 */
  F12: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51],

  // ============= 编辑区功能键 =============
  /** 打印屏幕键 */
  PRINT_SCREEN: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52],
  /** 滚动锁定键 */
  SCROLL_LOCK: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53],
  /** 暂停/中断键 */
  PAUSE_BREAK: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54],
  /** 插入键 */
  INSERT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55],
  /** Home键 */
  HOME: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56],
  /** 上翻页键 */
  PAGE_UP: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57],
  /** 删除键 */
  DELETE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58],
  /** End键 */
  END: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59],
  /** 下翻页键 */
  PAGE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A],

  // ============= 方向键 =============
  /** 右键头 */
  RIGHT_ARROW: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B],
  /** 左箭头 */
  LEFT_ARROW: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5C],
  /** 下箭头 */
  DOWN_ARROW: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5D],
  /** 上箭头 */
  UP_ARROW: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5E],

  // ============= 数字小键盘 =============
  /** 数字锁定键 */
  NUMLOCK: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F],
  /** 小键盘/除号键 */
  NUMPAD_DIVIDE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60],
  /** 小键盘*乘号键 */
  NUMPAD_MULTIPLY: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61],
  /** 小键盘-减号键 */
  NUMPAD_SUBTRACT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62],
  /** 小键盘+加号键 */
  NUMPAD_ADD: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63],
  /** 小键盘回车键 */
  NUMPAD_ENTER: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64],
  /** 数字键盘End键 */
  NUMPAD_END: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65],
  /** 数字键盘下箭头键 */
  NUMPAD: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66],
  /** 数字键盘PageDown键 */
  NUMPAD_PAGE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x67],
  /** 数字键盘左箭头键 */
  NUMPAD_LEFT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68],
  /** 数字键盘Clear键（NumLock关闭时的5键） */
  NUMPAD_CLEAR: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69],
  /** 数字键盘右箭头键 */
  NUMPAD_RIGHT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6A],
  /** 数字键盘Home键 */
  NUMPAD_HOME: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6B],
  /** 数字键盘上箭头键 */
  NUMPAD_UP: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C],
  /** 数字键盘PageUp键 */
  NUMPAD_PAGE_UP: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D],
  /** 数字键盘Insert键 */
  NUMPAD_INSERT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6E],
  /** 数字键盘Delete键 */
  NUMPAD_DELETE: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6F],
  /** 数字键盘应用程序键（上下文菜单键） */
  NUMPAD_APPS: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71],

  // ============= 控制键 =============
  /** 左边的Ctrl */
  LEFT_CTRL: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D],
  /** 左边的Shift */
  LEFT_SHIFT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E],
  /** 左边的Alt */
  LEFT_ALT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10],
  /** 左边的Win */
  LEFT_WIN: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15],
  
  /** 右边的Alt */
  RIGHT_ALT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4C],
  /** 右边的Shift */
  RIGHT_SHIFT: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C],
  /** 右边的Ctrl */
  RIGHT_CTRL: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1D],
  /** 右边的Win */
  RIGHT_WIN: [0x57, 0xAB, 0x00, 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15],
};

/**
 * 获取鼠标移动指令（相对值）
 * 每次只能移动0x7f(127像素点)，超过范围需要移动多次，所以返回的是指令列表
 * @param {number|string} _x - X轴移动距离（正数向右，负数向左）
 * @param {number|string} _y - Y轴移动距离（正数向下，负数向上）
 * @returns {Array} 鼠标移动指令数组
 * @throws {Error} 当输入值无效时抛出错误
 */
function getRelativeMouseMoveCommonds(_x, _y) {
  // 获取输入值并转换为数字
  let x = parseInt(_x);
  let y = parseInt(_y);

  if (isNaN(x) || isNaN(y)) {
    console.warn(`请输入有效数字`);
    return [];
  }

  const maxWidth = window.screen.width;
  const maxHeight = window.screen.height;

  if (x > maxWidth) x = maxWidth;
  if (x < -maxWidth) x = -maxWidth;
  if (y > maxHeight) y = maxHeight;
  if (y < -maxHeight) y = -maxHeight;

  // 分解移动距离为多个不超过127像素的移动
  const xMoves = decomposeMove(x);
  const yMoves = decomposeMove(y);
  
  // 计算需要生成的指令数量（取x和y方向移动次数的最大值）
  const maxSteps = Math.max(xMoves.length, yMoves.length);
  const commands = [];
  
  for (let i = 0; i < maxSteps; i++) {
    // 获取当前步的x和y移动距离（如果没有更多移动则为0）
    const currentX = i < xMoves.length ? xMoves[i] : 0;
    const currentY = i < yMoves.length ? yMoves[i] : 0;
    
    // 转换为字节表示
    const xByte = convertToByte(currentX);
    const yByte = convertToByte(currentY);
    
    // 构建基本数据帧(不含校验位)
    const baseData = [
      0x57, 0xAB, 0x00, 0x05, 0x05, 0x01, 0x00, 
      xByte, yByte, 0x00
    ];
    
    // 计算校验位
    const checksum = calculateChecksum(baseData);
    const fullData = [...baseData, checksum];
    commands.push(fullData);
  }
  
  return commands;
}

/**
 * 将移动距离分解为多个不超过127像素的移动
 * @param {number} distance - 总移动距离
 * @returns {Array} 分解后的移动距离数组
 */
function decomposeMove(distance) {
  const moves = [];
  let remaining = Math.abs(distance);
  const direction = distance < 0 ? -1 : 1;
  
  while (remaining > 0) {
    const step = Math.min(remaining, 127);
    moves.push(step * direction);
    remaining -= step;
  }
  
  return moves;
}

/**
 * 将移动距离转换为字节表示
 * @param {number} distance - 移动距离
 * @returns {number} 字节表示
 */
function convertToByte(distance) {
  if (distance === 0) return 0x00;
  if (distance > 0) return Math.min(distance, 127);
  // 负数转换为补码表示
  return 256 + Math.max(distance, -127);
}

/**
 * 相对移动
 * 命令太长，封装成函数
 */
async function relativeMouseMove(x, y) {
  return executePromisesInSequence(getRelativeMouseMoveCommonds(x, y).map(command => () => sendCommand(command)))
}

/**
 * 获取鼠标移动指令（绝对值）
 */
function getAbsoluteMouseMoveCommond(_x , _y) {
  // 获取输入值并转换为数字
  let x = parseInt(_x);
  let y = parseInt(_y);

  if (isNaN(x) || isNaN(y)) {
    console.warn(`请输入有效数字`);
    return [];
  }

  const maxWidth = window.screen.width;
  const maxHeight = window.screen.height;

  if (x > maxWidth) x = maxWidth;
  if (x < 0) x = 0;
  if (y > maxHeight) y = maxHeight;
  if (y < -maxHeight) y = 0;

  const x_correct = x * (4096 / maxWidth);
  const y_correct = y * (4096 / maxHeight);

  const x_low = x_correct & 0xFF;
  const x_hight = (x_correct >> 8) & 0xFF;

  const y_low = y_correct & 0xFF;
  const y_hight = (y_correct >> 8) & 0xFF;

  // 构建基本数据帧(不含校验位)
  const baseData = [
      0x57, 0xAB, 0x00, 0x04, 0x07, 0x02, 0x00, 
      x_low, x_hight,
      y_low, y_hight,
      0x00
  ];

  // 计算校验位
  const checksum = calculateChecksum(baseData);
  const fullData = [...baseData, checksum];
  return fullData;
}

/**
 * 计算校验和(所有字节相加取最后两位)
 * eg: calculateChecksum([0x57, 0xAB, 0x00, 0x05, 0x05, 0x01, 0x00]) => 0x13
 */
function calculateChecksum(dataArray) {
    let sum = 0;
    for (const byte of dataArray) {
        sum += byte;
    }
    return sum & 0xFF; // 取最后8位(1字节)
}

function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

/**
 * 同步执行多个promise
 */
async function executePromisesInSequence(promises) {
  const results = [];
  for (const promise of promises) {
    const result = await promise();
    results.push(result);
  }
  return results;
}

let count: 0 | 1 = 0;
/** 偏移点击 */
function task1() {
  setInterval(async () => {
    await relativeMouseMove(count === 0 ? 2 : -2, 0);

    await sendCommand(KeysMap.MOUSE_LEFT);
    await sendCommand(KeysMap.MOUSE_RELEASE);
    count = count === 0 ? 1 : 0;
  }, 2000)
}

async function test() {
  setTimeout(async () => {
    // 相对移动
    relativeMouseMove(-100, -100)
  }, 1000)

  // setTimeout(async () => {
  //   // 绝对移动
  //   await sendCommand(getAbsoluteMouseMoveCommond(200, 200));
  // }, 1000)
  
  // setInterval(async () => {
  //   await sendCommand(KeysMap.MOUSE_LEFT);
  //   await sendCommand(KeysMap.MOUSE_RELEASE);

  //   // await delay(1000);

  //   // await sendCommand(KeysMap.LEFT_SHIFT);
  //   // await sendCommand(KeysMap.KEY_RELEASE);

  //   count += 1;
  // }, 2000)
}

async function release() {
  await sendCommand(KeysMap.KEY_RELEASE);
}

onMounted(()=>{
    getValue()
})

defineExpose({
  
})
</script>

<style lang="scss" scoped>

</style>
